﻿<sticky:StickyWindowBase xmlns:sticky="clr-namespace:Squiggle.UI.StickyWindow"
    x:Class="Squiggle.UI.Windows.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:chat="clr-namespace:Squiggle.Client;assembly=Squiggle.Client"
    xmlns:controls="clr-namespace:Squiggle.UI.Controls"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        
    xmlns:tray="clr-namespace:Hardcodet.Wpf.TaskbarNotification"
    xmlns:local="clr-namespace:Squiggle.UI.Helpers"
    xmlns:converters="clr-namespace:Squiggle.UI.Converters"
    Title="{Binding Title}" Icon="/Squiggle;component/Images/Chat.ico" 
    Loaded="Window_Loaded" Closing="Window_Closing" 
    Width="303">
    <Window.Resources>
        <converters:AllEqualConverter x:Key="equalConverter" />
        <converters:StatusConverter x:Key="statusConverter" />
        <converters:TrayIconConverter x:Key="trayIconConverter" />
        <ObjectDataProvider MethodName="GetChangableStatuses" 
            ObjectType="{x:Type local:SquiggleUtility}" 
            x:Key="AvailableUserStatus">
        </ObjectDataProvider>
        <Style x:Key="onlineBuddiesMenu" TargetType="{x:Type MenuItem}">
            <Setter Property="IsEnabled" Value="{Binding AnyoneOnline}" />
        </Style>
        <Style x:Key="signedInMenu" TargetType="{x:Type MenuItem}">
            <Style.Setters>
                <Setter Property="IsEnabled" Value="{Binding IsLoggedIn}" />
            </Style.Setters>
        </Style>
        <Style x:Key="statusMenuItem" TargetType="{x:Type MenuItem}">
            <Style.Setters>
                <Setter Property="Header" Value="{Binding Converter={StaticResource statusConverter}}" />
                <Setter Property="IsChecked">
                    <Setter.Value>
                        <MultiBinding Converter="{StaticResource ResourceKey=equalConverter}">
                            <Binding RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type MenuItem}}" Path="DataContext.LoggedInUser.Status" />
                            <Binding Path="." />
                        </MultiBinding>
                    </Setter.Value>
                </Setter>
            </Style.Setters>
        </Style>
        <Style x:Key="statusMenu" TargetType="{x:Type MenuItem}" BasedOn="{StaticResource signedInMenu}">
            <Style.Setters>
                <Setter Property="ItemsSource" Value="{Binding Source={StaticResource AvailableUserStatus}}" />
                <Setter Property="ItemContainerStyle" Value="{StaticResource statusMenuItem}" />
            </Style.Setters>
        </Style>
        <Storyboard x:Key="blinkTrayIcon">
            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="trayIcon" Storyboard.TargetProperty="Tag">
                <DiscreteObjectKeyFrame Value="{x:Static Visibility.Collapsed}" KeyTime="0:0:0.25" />
                <DiscreteObjectKeyFrame Value="{x:Static Visibility.Visible}" KeyTime="0:0:0.5" />
                <DiscreteObjectKeyFrame Value="{x:Static Visibility.Collapsed}" KeyTime="0:0:0.75" />
                <DiscreteObjectKeyFrame Value="{x:Static Visibility.Visible}" KeyTime="0:0:1" />
                <DiscreteObjectKeyFrame Value="{x:Static Visibility.Collapsed}" KeyTime="0:0:1.25" />
                <DiscreteObjectKeyFrame Value="{x:Static Visibility.Visible}" KeyTime="0:0:1.5" />
                <DiscreteObjectKeyFrame Value="{x:Static Visibility.Collapsed}" KeyTime="0:0:1.75" />
                <DiscreteObjectKeyFrame Value="{x:Static Visibility.Visible}" KeyTime="0:0:2" />
                <DiscreteObjectKeyFrame Value="{x:Static Visibility.Collapsed}" KeyTime="0:0:2.25" />
                <DiscreteObjectKeyFrame Value="{x:Static Visibility.Visible}" KeyTime="0:0:2.5" />
                <DiscreteObjectKeyFrame Value="{x:Static Visibility.Collapsed}" KeyTime="0:0:2.75" />
                <DiscreteObjectKeyFrame Value="{x:Static Visibility.Visible}" KeyTime="0:0:3" />
            </ObjectAnimationUsingKeyFrames>
        </Storyboard>
    </Window.Resources>
    <Grid x:Name="LayoutRoot" FlowDirection="{StaticResource flowDirection}">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition />
        </Grid.RowDefinitions>
        <tray:TaskbarIcon TrayLeftMouseDown="trayIcon_TrayLeftMouseDown" x:Name="trayIcon" ToolTipText="{Binding LoggedInUser.Status, FallbackValue=Offline}" >
            <tray:TaskbarIcon.IconSource>
                <MultiBinding Converter="{StaticResource trayIconConverter}">
                    <Binding Path="LoggedInUser.Status" FallbackValue="Offline" />
                    <Binding RelativeSource="{RelativeSource Self}" Path="Tag" />
                </MultiBinding>
            </tray:TaskbarIcon.IconSource>
            <tray:TaskbarIcon.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="{StaticResource MainWindow_TrayMenu_Open}" Click="OpenMenu_Click" />
                    <MenuItem Header="{StaticResource MainWindow_TrayMenu_ChangeStatus}"
                              MenuItem.Click="StatusMenu_Click"
                              Style="{StaticResource statusMenu}" />
                    <MenuItem Header="{StaticResource MainWindow_TrayMenu_SignOut}" 
                              Click="SignOutMenu_Click" 
                              Style="{StaticResource signedInMenu}" />
                    <MenuItem Header="{StaticResource MainWindow_TrayMenu_Quit}" Click="QuiteMenu_Click" />
                </ContextMenu>
            </tray:TaskbarIcon.ContextMenu>
        </tray:TaskbarIcon>
        <Menu Grid.Row="0">
            <Menu.Background>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="#FFEFEEF3" Offset="0.125"/>
                    <GradientStop Color="#FFD8DDE6" Offset="0.312"/>
                    <GradientStop Color="#FFECECEC" Offset="0.875"/>
                </LinearGradientBrush>
            </Menu.Background>
            <MenuItem Header="{StaticResource MainWindow_MainMenu_File}">
                <MenuItem Header="{StaticResource MainWindow_MainMenu_SignOut}" 
                          Click="SignOutMenu_Click" 
                          Style="{StaticResource signedInMenu}" />
                <Separator />
                <MenuItem Header="{StaticResource MainWindow_MainMenu_Status}" 
                          Style="{StaticResource statusMenu}"
                          MenuItem.Click="StatusMenu_Click" />
                <Separator />
                <MenuItem Header="{StaticResource MainWindow_MainMenu_OpenFiles}" MenuItem.Click="OpenReceivedFilesMenu_Click" />
                <Separator />
                <MenuItem Header="{StaticResource MainWindow_MainMenu_Close}" MenuItem.Click="CloseMenu_Click" />
            </MenuItem>
            <MenuItem Header="{StaticResource MainWindow_MainMenu_Actions}">
                <MenuItem Header="{StaticResource MainWindow_MainMenu_SendMessage}" Style="{StaticResource onlineBuddiesMenu}" Click="SendMessageMenu_Click" />
                <MenuItem Header="{StaticResource MainWindow_SendBroadcastMessage}" Style="{StaticResource onlineBuddiesMenu}" Click="SendBroadcastMessageMenu_Click" />
                <MenuItem Header="{StaticResource MainWindow_MainMenu_SendFile}" Style="{StaticResource onlineBuddiesMenu}" Click="SendFileMenu_Click" />
            </MenuItem>
            <MenuItem Header="{StaticResource MainWindow_MainMenu_Tools}">
                <MenuItem x:Name="mnuGroupBuddies" Header="{StaticResource MainWindow_MainMenu_GroupBuddies}" MenuItem.Click="GroupMenu_Click" Style="{StaticResource signedInMenu}" />
                <MenuItem Header="{StaticResource MainWindow_MainMenu_SortBuddies}" Style="{StaticResource signedInMenu}">
                    <MenuItem x:Name="mnuSortByName" Header="{StaticResource MainWindow_MainMenu_SortByName}" MenuItem.Click="SortMenu_Click" Tag="DisplayName" />
                    <MenuItem x:Name="mnuSortByStatus" Header="{StaticResource MainWindow_MainMenu_SortByStatus}"  MenuItem.Click="SortMenu_Click" Tag="Status" />
                </MenuItem>
                <MenuItem Header="{StaticResource MainWindow_MainMenu_HistoryViewer}" MenuItem.Click="HistoryViewerMenu_Click" />
                <MenuItem Header="{StaticResource MainWindow_MainMenu_Settings}" MenuItem.Click="SettingsMenu_Click" />
            </MenuItem>
            <MenuItem Header="{StaticResource MainWindow_MainMenu_Help}">
                <MenuItem Header="{StaticResource Global_Menu_About}" MenuItem.Click="AboutMenu_Click" />
            </MenuItem>
        </Menu>        
        <controls:ChatClientControl Grid.Row="1" Width="Auto" Height="Auto" x:Name="chatControl" />
        <controls:BusyIndicator x:Name="busyIndicator" Grid.Row="1" Message="{StaticResource MainWindow_PleaseWait}" />
    </Grid>
</sticky:StickyWindowBase>
